package com.ctlele.team.space.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.ctlele.team.space.R;
import com.squareup.picasso.Picasso;

public class MsgDialog extends Dialog {

    private static final int ICON_SUCCESS = R.mipmap.success;

    private static final int ICON_FAILED = R.mipmap.failed;

    private static final int ICON_ALERT = R.mipmap.alert_single;

    public static final int WINDOW_LOCATION_TOP = Gravity.TOP;

    public static final int WINDOW_LOCATION_BOTTOM = Gravity.BOTTOM;

    public static final int WINDOW_LOCATION_CENTER = Gravity.CENTER;

    private ImageView mIcon;

    private TextView mMsg;

    private final Handler mCloser = new Handler();

    private int mDelay = 2000;

    private int mShowLocation = WINDOW_LOCATION_TOP;

    private Runnable mAfterDismiss;

    public MsgDialog(@NonNull Context context) {
        super(context);
        init();
    }

    public MsgDialog(@NonNull Context context, int delay) {
        super(context);
        mDelay = delay;
        init();
    }

    public MsgDialog delay(int mDelay) {
        this.mDelay = mDelay;
        return this;
    }

    public MsgDialog showLocation(int mShowLocation) {
        this.mShowLocation = mShowLocation;
        return this;
    }

    public MsgDialog afterDismiss(Runnable mAfterDismiss) {
        this.mAfterDismiss = mAfterDismiss;
        return this;
    }

    private void init() {
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setDimAmount(0.3f);
        getWindow().setGravity(mShowLocation);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(initLayout());
        initView();
    }

    protected int initLayout() {
        return R.layout.dialog_msg_layout;
    }

    protected void initView() {
        mIcon = findViewById(R.id.msg_icon);
        mMsg = findViewById(R.id.msg);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mCloser != null) {
            mCloser.postDelayed(this::dismiss, mDelay);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        afterDismiss();
    }

    private void afterDismiss() {
        if (mAfterDismiss != null) {
            mAfterDismiss.run();
        }
    }

    public MsgDialog success(String msg) {
        return msg(msg, ICON_SUCCESS);
    }

    public MsgDialog failed(String msg) {
        return msg(msg, ICON_FAILED);
    }

    public MsgDialog alert(String msg) {
        return msg(msg, ICON_ALERT);
    }

    public MsgDialog msg(String msg, int icon) {
        mIcon.setImageResource(icon);
        mMsg.setText(msg);
        return this;
    }

    public MsgDialog msg(String msg, Bitmap icon) {
        mIcon.setImageBitmap(icon);
        mMsg.setText(msg);
        return this;
    }

    public MsgDialog msg(String msg, String iconUrl) {
        Picasso.with(getContext()).load(iconUrl).into(mIcon);
        mMsg.setText(msg);
        return this;
    }
}
